package dao.Impl;

import dao.PetOwnerDao;
import entity.PetOwner;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author wfr
 * @description:
 * @date 2019-05-31 15:12
 * @parem PetOwnerDaoImpl
 */
public class PetOwnerDaoImpl extends BaseDao  implements PetOwnerDao {
    public PetOwnerDaoImpl() {super();
    }

    @Override
    public int add(PetOwner petOwner) throws SQLException {
        int r=0;
        try {

            PreparedStatement pst =con.prepareStatement("insert into petowner(NAME,PASSWORD,MONEY) value (?,?,?)");
            pst.setString(1,petOwner.getName());
            pst.setString(2,petOwner.getPassword());
            pst.setInt(3,petOwner.getMoney());
            r=pst.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        return r;
    }
    @Override
    public PetOwner getBtId(int id) {
        PetOwner petOwner=null;
        try {

            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery("select * from petowner where ID="+id);
            if (resultSet.next())
            {
                petOwner=new PetOwner();
                petOwner.setId(resultSet.getInt(1));
                petOwner.setName(resultSet.getString(2));
                petOwner.setPassword(resultSet.getString(3));
                petOwner.setMoney(resultSet.getInt(4));
            }


        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        return petOwner;
    }
    @Override
    public List<PetOwner> getlist() {
        List<PetOwner> petOwners = new ArrayList<PetOwner>();
        try {
            String sql = "select * from petowner";
            PetOwner petOwner = null;
            PreparedStatement pst = con.prepareStatement(sql);
            // 设置条件(相对于条件查询)
            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery(sql);

            while (resultSet.next()) {
                petOwner= new PetOwner();
                petOwner.setId(resultSet.getInt(1));
                petOwner.setName(resultSet.getString(2));
                petOwner.setPassword(resultSet.getString(3));
                petOwner.setMoney(resultSet.getInt(4));
                petOwners.add(petOwner);
            }

        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return petOwners;
    }
    @Override
    public int updata(int id, int money) {
        String sql = "update petowner set MONEY=? where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, money);
            pst.setInt(2, id);
            pst.executeUpdate();
            System.out.println("更新成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return 0;
    }
    @Override
    public int delete(int id) {
        String sql = "delete from petowner where ID=?";

        PreparedStatement pst;
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);
            pst.executeUpdate();
            System.out.println("删除成功");
            return 1;
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return 0;
    }
    @Override
    public PetOwner login(int account, String password) {
        PetOwner petOwner = null;
        try {

            ResultSet resultSet;
            String sql="select * from petowner where ID=? and PASSWORD=?";
            PreparedStatement pst;
            pst = con.prepareStatement(sql);
            pst.setInt(1, account);
            pst.setString(2, password);
            resultSet=pst.executeQuery();
            if (resultSet.next()) {
                petOwner = new PetOwner();
                petOwner.setId(resultSet.getInt(1));
                petOwner.setName(resultSet.getString(2));
                petOwner.setPassword(resultSet.getString(3));
                petOwner.setMoney(resultSet.getInt(4));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return petOwner;
    }
}
